﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace sistem
{
    public partial class DodavanjeKabinetaForma : Form
    {
        string slika = "";
        public DodavanjeKabinetaForma()
        {
            InitializeComponent();
        }

        private bool ValidacijaKabineta()
        {
            bool trigger = true;
            if (textBox1.Text == "")
            {
                trigger = false;
                errorProvider1.SetError(textBox1, "Naziv kabineta je obavezan");
            }
            return trigger;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                bool var1 = ValidacijaKabineta();
                if (var1)
                {
                    Kabinet kabinet = new Kabinet();
                    kabinet.Naziv = textBox1.Text;
                    kabinet.Sprat = (int)numericUpDown1.Value;
                    kabinet.Kapacitet = (int)numericUpDown2.Value;
                    kabinet.Slika = slika;
                    DAL.DAL d = DAL.DAL.Instanca;
                    d.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                    d.getDAO.getKabinetDAO().create(kabinet);
                    d.terminirajKonekciju();
                    MessageBox.Show("Kabinet je uspješno dodan u bazu podataka", "Poruka", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }
            catch (MySqlException)
            {
                errorProvider1.SetError(textBox1, "Kabinet sa istim nazivom već postoji");
            }
            catch (Exception)
            {
                MessageBox.Show("Došlo je do pogreške prilikom rada s bazom podataka", "Greška", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        protected static Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(ofd.FileName);
                pictureBox1.Image = resizeImage(pictureBox1.Image, new Size(177, 101));
                slika = ofd.FileName;
            }
        }
    }
}
